package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

public class StudentDao implements BaseStudentDao{
   private static Student[] stu =new Student[5];
   static {
       Student stu1=new Student("heima001","李丹丹","23","1999");
       Student stu2=new Student("heima002","李文杰","23","1999");
       stu[0]=stu1;
       stu[1]=stu2;
   }
    public boolean addStudenet(Student student) {
        int index=-1;
        for (int i = 0; i < stu.length; i++) {
            if(stu[i]==null){
                index=i;
                break;
            }
        }
        if(index==-1){
            return false;
        }else{
            //不要忘了存数据
            stu[index]=student;
            return true;
        }
    }

    public Student[] findAllStudent() {
        return stu;
    }
//删除学生
    public void deleteStudent(String id) {
       int index =getIndex(id);
       stu[index]=null;
    }

    //获取指定学号在数组中的索引
    public int getIndex(String id){
        int index=-1;
        for (int i = 0; i < stu.length; i++) {
            if (stu[i]!=null &&id.equals(stu[i].getId())){
                index=i;
            }
        }
        return index;
    }
//修改学生
    public void updateStudent(Student student, String id) {
        int index=getIndex(id);
        stu[index]=student;
    }
}
